package com.example.a1725185505.works.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a1725185505.works.R;
import com.example.a1725185505.works.base.BaseListener;
import com.example.a1725185505.works.bean.LoginBean;
import com.example.a1725185505.works.model.LoginModel;
import com.example.a1725185505.works.util.FullScreen;
import com.example.a1725185505.works.util.SharedPreferencesUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button bt_login_denglu;
    private EditText et_username,et_password;
    private TextView tv_new_register;
    private SharedPreferencesUtils sharedPreferencesUtils;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FullScreen.fullScreen(this,0x33131313);
        setContentView(R.layout.activity_main);
        bt_login_denglu = (Button)findViewById(R.id.bt_denglu);
        bt_login_denglu.setOnClickListener(this);
        et_username = (EditText)findViewById(R.id.et_username);
        et_password = (EditText)findViewById(R.id.et_password);
        tv_new_register = (TextView)findViewById(R.id.tv_register);
        tv_new_register.setOnClickListener(this);
        context = getApplicationContext(); //上下文
        sharedPreferencesUtils = new SharedPreferencesUtils(context);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_register:
                Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_denglu:
                String yonghuming = et_username.getText().toString();
                String mima = et_password.getText().toString();
                LoginModel loginModel = new LoginModel();
                loginModel.login(yonghuming,mima,loginlistener);



                break;
        }
    }


    BaseListener<LoginBean> loginlistener = new BaseListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if (loginBean.getUserid().equals("0")){
                Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_SHORT).show();
            }
            else {
                sharedPreferencesUtils.saveString("userid",loginBean.getUserid());
                Intent intent1 = new Intent(MainActivity.this,HomeActivity.class);
                startActivity(intent1);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
        }
    };
}
